/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package manufacturerBean;

import DBConnection.ATeamObject;
import java.sql.*;
import java.rmi.RemoteException;
import java.util.*;

import javax.ejb.*;
import javax.sql.*;
import javax.naming.*;
/**
 *
 * @author Konstantin
 */
public class ManufacturerBean implements EntityBean{
    
    private EntityContext entityContext;
    private Connection connection;
    private DataSource dataSource;
    
    private Integer ObjectId;
    private String Name;
    private String ObjectDescription;
    private Integer ParentId;
    
    public ManufacturerBean(){
        
    }
    
    public Integer getObjectId()
            throws RemoteException{
        return ObjectId;
    }

    public void setName(ATeamObject ATObj)
            throws RemoteException{
        Name = ATObj.getName();
    }
    
    public String getName()
            throws RemoteException{
        return Name;
    }
    
    public void setObjectDescription(ATeamObject ATObj)
            throws RemoteException{
        ObjectDescription = ATObj.getObjectDescription();
    }
    
    public String getObjectDescription()
            throws RemoteException{
        return ObjectDescription;
    }
    
    public void setParentId(ATeamObject ATObj)
            throws RemoteException{
        ParentId = new Integer(ATObj.getParentId());
    }
    
    public Integer getParentId()
            throws RemoteException{
        return ParentId;
    }
    
    public Integer ejbFindByPrimaryKey(Integer primaryKey) 
            throws RemoteException, FinderException{
        try{
            String findByPrimaryKeyQuery = "SELECT Object.ObjectId FROM Object"+
                    " WHERE Object.ObjectId = ?";
            
            if (connection == null){
                connection = dataSource.getConnection();
            }
            PreparedStatement findByPrimaryKeyQueryStat = 
                    connection.prepareStatement(findByPrimaryKeyQuery);
            findByPrimaryKeyQueryStat.setInt(1, primaryKey.intValue());
            ResultSet res = findByPrimaryKeyQueryStat.executeQuery();
            if(res.next()){
                res.close();
                findByPrimaryKeyQueryStat.close();
                closeConnection();
                return primaryKey;
            }
            else{
                throw new FinderException("Category hasn't been found");
            } 
        }
        catch(SQLException sqlException){
            throw new FinderException(sqlException.getMessage());
        } 
    }
    
    public Collection ejbFindByName(ATeamObject ATObj)
            throws RemoteException, FinderException{
        try{
            String findByNameQuery = "SELECT Object.ObjectId FROM Object"+
                    " WHERE Object.Name = ?";
            
            if (connection == null){
                connection = dataSource.getConnection();
            }
            PreparedStatement findByNameQueryStat =
                    connection.prepareStatement(findByNameQuery);
            findByNameQueryStat.setString(1, ATObj.getName());
            ResultSet res = findByNameQueryStat.executeQuery();
            Collection Keys = new ArrayList();
            if (!res.next()) {
                throw new FinderException("Manufacturer hasn't been found.");
            }
            else{
                do{
                    Keys.add(new Integer(res.getInt("ObjectId")));
                }while(res.next());
                res.close();
                findByNameQueryStat.close();
                closeConnection();
                return Keys;
            }
            
        }
        catch(SQLException sqlException){
            throw new FinderException(sqlException.getMessage());
        }
    }
    
    public Collection ejbFindByObjectDescription(ATeamObject ATObj)
            throws RemoteException, FinderException{
        try{
            String findByObjectDescriptionQuery = "SELECT Object.ObjectId FROM Object"+
                    " WHERE Object.ObjectDescription = ?";
            
            if (connection == null){
                connection = dataSource.getConnection();
            }
            PreparedStatement findByObjectDescriptionQueryStat =
                    connection.prepareStatement(findByObjectDescriptionQuery);
            findByObjectDescriptionQueryStat.setString(1, ATObj.getObjectDescription());
            ResultSet res = findByObjectDescriptionQueryStat.executeQuery();
            Collection Keys = new ArrayList();
            if (!res.next()) {
                throw new FinderException("Manufacturer hasn't been found.");
            }
            else{
                do{
                    Keys.add(new Integer(res.getInt("ObjectId")));
                }while(res.next());
                res.close();
                findByObjectDescriptionQueryStat.close();
                closeConnection();
                return Keys;
            }
            
        }
        catch(SQLException sqlException){
            throw new FinderException(sqlException.getMessage());
        }
    }
    
    public Collection ejbFindAll()
            throws RemoteException, FinderException{
        try{
            String findAllQuery = "SELECT Object.ObjectId FROM Object"+
                    " WHERE Object.ObjectTypeId = 2";
            
            if (connection == null){
                connection = dataSource.getConnection();
            }
            PreparedStatement findAllQueryStat =
                    connection.prepareStatement(findAllQuery);
            ResultSet res = findAllQueryStat.executeQuery();
            Collection Keys = new ArrayList();
            if (!res.next()) {
                throw new FinderException("Manufacturer hasn't been found.");
            }
            else{
                do{
                    Keys.add(new Integer(res.getInt("ObjectId")));
                }while(res.next());
                res.close();
                findAllQueryStat.close();
                closeConnection();
                return Keys;
            }
            
        }
        catch(SQLException sqlException){
            throw new FinderException(sqlException.getMessage());
        }
    }
    
    public Integer ejbCreate(ATeamObject ATObj) 
            throws CreateException, RemoteException{
        try{
            /*String insert ="BEGIN" + 
                " INSERT INTO Object"+
                " VALUES (Objectseq.nextval, 26, ?, 1, ?);"+
                " INSERT INTO Params"+
                " VALUES (Objectseq.currval, 2, ?);" +
                " END";
            */
            String getNextSeq = "SELECT ObjectSeq.nextval FROM dual";
            
            if (connection == null){
                connection = dataSource.getConnection();
            }
            PreparedStatement getNextSeqStat = 
                    connection.prepareStatement(getNextSeq);
            ResultSet res = getNextSeqStat.executeQuery();
            res.next();
            int nextseq = res.getInt(1);
            getNextSeqStat.close();
            ObjectId = new Integer(nextseq);
            
            String insertIntoObject = "INSERT INTO Object"+
                    " VALUES (?, 26, ?, 2, ?)";
            
            String insertIntoParams = "INSERT INTO Params"+
                    " VALUES (?, 4, ?)";
            
            this.Name = ATObj.getName();
            this.ObjectDescription = ATObj.getObjectDescription();
            this.ParentId = 26;
            
            PreparedStatement insertStatementObject =
                    connection.prepareStatement(insertIntoObject);
            insertStatementObject.setInt(1, nextseq);
            insertStatementObject.setString(2, ATObj.getName());
            insertStatementObject.setString(3, ATObj.getObjectDescription());
            insertStatementObject.executeUpdate();
            insertStatementObject.close();
            
            PreparedStatement insertStatementParams =
                    connection.prepareStatement(insertIntoParams);
            insertStatementParams.setInt(1, nextseq);
            insertStatementParams.setString(2, ATObj.getName());
            insertStatementParams.executeUpdate();
            insertStatementParams.close();
            
            closeConnection();
            return ObjectId;
        }
        catch(SQLException sqlException){
            throw new CreateException(sqlException.getMessage());
        }
    }
    
    public void ejbPostCreate(ATeamObject ATObj){
        
    }
    
    public void setEntityContext(EntityContext ctx)
            throws EJBException, RemoteException {
        entityContext = ctx;
        try{
            InitialContext initialContext = new InitialContext();
            
            dataSource =
                (DataSource) initialContext.lookup("java:/ATeamConnectionPool");           
            connection = dataSource.getConnection();
        }
        catch(NamingException namingException ){
            throw new EJBException( namingException);
        }
        catch(SQLException sqlException){
            throw new EJBException(sqlException);
        }
    }

    public void unsetEntityContext()
            throws EJBException, RemoteException {
        entityContext = null;
        try{
            connection.close();
        }
        catch(SQLException sqlException){
            throw new EJBException(sqlException);
        }
        finally{
            connection = null;
        }
    }

    public void ejbRemove()
            throws RemoveException, EJBException, RemoteException {
        try{
            Integer primaryKey = (Integer) entityContext.getPrimaryKey();
            
            String delete = "DELETE FROM Object WHERE"+
                    " Object.ObjectId = ?";
            
            if (connection == null){
                connection = dataSource.getConnection();
            }
            PreparedStatement deleteStatement = connection.prepareStatement(delete);
            
            deleteStatement.setInt(1, primaryKey.intValue());
            
            deleteStatement.executeUpdate();
            deleteStatement.close();
            closeConnection();
        }
        catch(SQLException sqlException){
            throw new RemoveException(sqlException.getMessage());
        }
    }

    public void ejbActivate()
            throws EJBException, RemoteException {
        
    }

    public void ejbPassivate() 
            throws EJBException, RemoteException {
        
    }

    public void ejbLoad() 
            throws EJBException, RemoteException {
        try{
            Integer primaryKey = (Integer) entityContext.getPrimaryKey();
            
            String select = "SELECT * FROM Object WHERE"+
                    " Object.ObjectId = ?";
            
            if (connection == null){
                connection = dataSource.getConnection();
            }
            PreparedStatement selectStatement = connection.prepareStatement(select);
            
            selectStatement.setInt(1, primaryKey.intValue());
            
            ResultSet resultSet = selectStatement.executeQuery();
            
            if(resultSet.next()){
                ObjectId = new Integer(resultSet.getInt("ObjectId"));
                Name = resultSet.getString("Name");
                ObjectDescription = resultSet.getString("ObjectDescription");
                ParentId = new Integer(resultSet.getInt("ParentId"));
            }
            else 
                throw new EJBException("No such manufacturer.");
            
            selectStatement.close();
            closeConnection();
        }
        catch(SQLException sqlException){
            throw new EJBException(sqlException);
        }
    }

    public void ejbStore() 
            throws EJBException, RemoteException {
        try{
            Integer primaryKey = (Integer) entityContext.getPrimaryKey();
            
            String editNameOfCategoryInObjectQuery = 
                "UPDATE Object"+ 
                " SET Object.Name = ?"+
                " WHERE Object.ObjectId = ?";
    
            String editNameOfCategoryInParamsQuery = 
                "UPDATE Params"+ 
                " SET Params.Value = ?"+
                " WHERE Params.ObjectId = ?"+
                " AND Params.AttrId = 4";
            
            String editDescriptionOfCategoryQuery =
                "UPDATE Object"+
                " SET Object.ObjectDescription = ?"+
                " WHERE Object.ObjectId = ?";
            
            if (connection == null){
                connection = dataSource.getConnection();
            }
            PreparedStatement updateStatement =
                    connection.prepareStatement(editNameOfCategoryInObjectQuery);         
            updateStatement.setString(1, Name);
            updateStatement.setInt(2, primaryKey.intValue());
            updateStatement.executeUpdate();
            
            updateStatement =
                    connection.prepareStatement(editNameOfCategoryInParamsQuery);
            updateStatement.setString(1, Name);
            updateStatement.setInt(2, primaryKey.intValue());
            updateStatement.executeUpdate();
            
            updateStatement =
                    connection.prepareStatement(editDescriptionOfCategoryQuery);
            updateStatement.setString(1, ObjectDescription);
            updateStatement.setInt(2, primaryKey.intValue());
            updateStatement.executeUpdate();
            
            updateStatement.close();
            closeConnection();
        }
        catch(SQLException sqlException){
            throw new EJBException(sqlException);
        }
    }
    
    void closeConnection() {
        try {
            if (connection != null){
                connection.close();
                System.out.println("Connection closed");
            }
        } catch (SQLException e) {
            throw new EJBException("closeConnection:" + e.getMessage());
        } finally {
            connection = null;
        }
    }
}
